﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using XRM.Security.DataAccess.Interfaces;

namespace XRM.Security.DataAccess.Models
{
    [Table("CertRegister")]
    public class CertRegister : ICertificate, IUserCertStatus
    {
        public CertRegister()
        {
            CertRegisterInfoSystems = new List<CertRegisterInfoSystem>();   
        }

        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int CertRegisterId { get; set; }

        [Required]
        [MaxLength(255)]
        public string FileName { get; set; }

        public byte[] FileData { get; set; }

        [Required]
        public int UserRegisterId { get; set; }
        [Required]
        public int IdentityCenterId { get; set; }
        public int? PCId { get; set; }

        [Required]
        public DateTime UploadDate { get; set; }
        [Required]
        public DateTime FromDate { get; set; }
        [Required]
        public DateTime ToDate { get; set; }

        [NotMapped]
        public int UserStatus
        {
            get
            {
                if (UserRegister != null)
                    return UserRegister.Status;
                else
                    return (int)XRM.Security.DataAccess.Enums.UserStatus.Work;
            }
        }

        [Required]
        [MaxLength(50)]
        public string SerialNumber { get; set; }
        [MaxLength(50)]
        public string SerialNumberEtoken { get; set; }
        [Required]
        [MaxLength(50)]
        public string Thumbprint { get; set; }

        public int? RequestId { get; set; }

        [ForeignKey("IdentityCenterId")]
        public virtual IdentityCenter IdentityCenter { get; set; }
        [ForeignKey("UserRegisterId")]
        public virtual UserRegister UserRegister { get; set; }
        [ForeignKey("PCId")]
        public virtual PC PC { get; set; }
        [ForeignKey("RequestId")]
        public virtual Request Request { get; set; }

        public virtual ICollection<CertRegisterInfoSystem> CertRegisterInfoSystems { get; set; }
    } 
}